<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>WebSocket Demo</title>
    <style>
      .log {
        font-family: monospace;
        margin: 0;
        margin-bottom: 1em;
        padding: 1em;
        background-color: #dedede;
      }
    </style>
  </head>
  <body>
    <div id="app"></div>
    <script src="https://unpkg.com/mqtt@4.3.6/dist/mqtt.min.js"></script>
    <script>
      const app = document.querySelector('#app');
      const client = mqtt.connect('ws://202.38.75.252:8083/mqtt');

      function appendLog(log) {
        const dom = document.createElement('p');
        dom.className = 'log';
        dom.innerText = log;
        app.appendChild(dom);
      }

      client.subscribe('testtopic/#');

      client.on('connect', () => {
        appendLog('connected to ws://202.38.75.252:8083/mqtt');
      });

      client.on('message', (topic, payload) => {
        appendLog([topic, payload].join(': '));
      });

      setInterval(() => {
        client.publish('testtopic', 'Hello World');
      }, 1000);
    </script>
  </body>
</html>
